﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using WcfService.Util;
using System.Data;
using WcfService.DTO;

namespace WcfService.DAO
{
    public class BinhLuanDAO:DataProvider
    {
        public BinhLuanDAO() {
            connect();
        }
         public BinhLuanDTO[] getAll()
        {
            string query = "SELECT bl.MaBinhLuan, bl.NguoiBinhLuan, bl.MaTin, bl.NoiDung, bl.TinhTrang, tmb.TieuDe FROM BinhLuan bl INNER JOIN TinMuaBan tmb ON bl.MaTin = tmb.MaTinMuaBan";
            adapter = new OleDbDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            BinhLuanDTO[] arr;

            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            arr = new BinhLuanDTO[n];
            for (i = 0; i < n; i++)
            {
                object bl = GetDataFromDataRow(dt, i);
                arr[i] = (BinhLuanDTO)bl;
            }
            return arr;
        }
         public BinhLuanDTO[] getComment(string ma)
         {
             int id = Int32.Parse(ma);
             string query = "SELECT * FROM BinhLuan WHERE MaTin="+id;
             adapter = new OleDbDataAdapter(query, connection);
             DataSet dataset = new DataSet();
             adapter.Fill(dataset);
             BinhLuanDTO[] arr;

             DataTable dt = dataset.Tables[0];
             int i, n = dt.Rows.Count;
             arr = new BinhLuanDTO[n];
             for (i = 0; i < n; i++)
             {
                 object bl = GetDataFromDataRow(dt, i);
                 arr[i] = (BinhLuanDTO)bl;
             }
             return arr;
         }
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            BinhLuanDTO bl = new BinhLuanDTO();
            bl.MaBinhLuan = int.Parse(dt.Rows[i]["MaBinhLuan"].ToString());
            bl.NguoiBinhLuan = dt.Rows[i]["NguoiBinhLuan"].ToString();
            bl.MaTin = int.Parse(dt.Rows[i]["MaTin"].ToString());
            bl.NoiDung = dt.Rows[i]["NoiDung"].ToString();
            bl.TinhTrang = int.Parse(dt.Rows[i]["TinhTrang"].ToString());

            return (object)bl;
        }

        public void insert(string nguoiBL, int maTin, string noiDung, int tinhTrang)
        {
            string insertCommand = "INSERT INTO BinhLuan VALUES('" +
                                   nguoiBL + "'," +
                                   maTin + ", '" +
                                   noiDung + "," +
                                   tinhTrang + ")";

            executeNonQuery(insertCommand);
            disconnect();
        }

        public void delete(int maBL)
        {
            string deleteCommand = "UPDATE BinhLuan SET TinhTrang = 0 WHERE MaBinhLuan = " + maBL;
            executeNonQuery(deleteCommand);
            disconnect();
        }

        public void update(int ma, string nguoiBL, int maTin, string noiDung, int tinhTrang)
        {
            string updateCommand = "UPDATE BinhLuan SET NguoiBinhLuan = '" + nguoiBL + "',"
                + "MaTin = " + maTin + ","
                 + "NoiDung = '" + noiDung + "',"
                + "TinhTrang = " + tinhTrang 
                + "WHERE MaBinhLuan = " + ma;
            executeNonQuery(updateCommand);
            disconnect();
        }
    }
}